// pages/setting/setting.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    speed: app.globalData.audioSpeed,
    play: app.globalData.playLeave,
    count: app.globalData.selfData.length,
    currentValue: app.globalData.playLeave,
    show: false,
    popShow: false,
    columns: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
    pickerIndex: 9
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.initData()
  },
//this.getArrayIndex(this.data.columns, app.globalData.audioSpeed)
  initData() {
    this.setData({
      speed: app.globalData.audioSpeed,
      play: app.globalData.playLeave,
      count: app.globalData.selfData.length,
      currentValue: app.globalData.playLeave,
    })
    
    var picker = this.selectComponent('.picker');
    setTimeout(()=>{
        picker.setIndexes([ this.getArrayIndex(this.data.speed) ])
    },0)
  },

  getArrayIndex(array, value) {
    for (var i = 0; i < array.length; i++) {
      if (array[i] === value) {
        return i;
      }
    }
    return 0;
  },

  clickSpeed() {
    this.setData({
      popShow: true
    })
  },

  clickPlay() {
    this.setData({
      show: true,
    })
  },

  clickClear() {
    wx.showModal({
      title: '清除数据',
      content: '是否清除自定义功能的所有数据？',
      complete: (res) => {
        if (res.confirm) {
          wx.setStorage({
            key: "selfData",
            data: []
          })
          this.setData({
            count: 0,
          });
          //刷新globalData
          app.globalData.selfData = []
        }
      }
    })

  },
  onDrag(event) {
    this.setData({
      currentValue: event.detail,
    });
  },
  onClickHide() {
    this.setData({
      show: false
    });
    this.setData({
      currentValue: this.data.play
    });
  },
  clickSure() {
    this.setData({
      show: false,
      play: this.data.currentValue
    });

    wx.setStorage({
        key: "playLeave",
        data: this.data.currentValue
      }),
      app.globalData.playLeave = this.data.currentValue
    console.log(app.globalData.playLeave)
  },
  onPopClose() {
    this.setData({
      popShow: false
    })
  },
  onPickerChange(event) {
    const {
      picker,
      value,
      index
    } = event.detail;
    this.setData({
        speed: value
      }),
      wx.setStorage({
        key: "audioSpeed",
        data: value
      })
    app.globalData.audioSpeed = value
  }
})